package com.ellricky.teacher.ui.mycourse.course

import android.view.View
import android.widget.AdapterView
import com.andr.common.http.callback.BaseCallback
import com.andr.common.http.callback.JOCallback
import com.andr.common.http.util.JsonUtil
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RefreshFlag
import com.ellricky.baselib.ui.base.BaseRefreshActivity
import com.ellricky.teacher.*
import org.json.JSONObject

class WeekCourseListActivity : BaseRefreshActivity<Void, BasePresenter<Void>, MyCourseEntity>() {

    override fun initUIView() {
        super.initUIView()
        mTvTitle.setText(R.string.tc_cur_week_course)
    }

    override fun setRefreshListener() {
        rvView.init(this, RefreshFlag.REFRESH)
    }

    override fun initParamMap() {

    }


    override fun getResponseCallback(): BaseCallback {
        return object : JOCallback(this) {
            override fun onSuccess(dataJO: JSONObject?, msg: String?) {
                val allCourseList = mutableListOf<MyCourseEntity>()
                dataJO?.let {
                    val dataJA = it.optJSONArray("data")
                    if (dataJA != null && dataJA.length() > 0) {
                        for (i in 0 until dataJA.length()) {
                            val itemJo = dataJA.optJSONObject(i)
                            val week = itemJo.optString("week")
                            val weekDay = itemJo.optString("weekDay")

                            val weekCourseJA = itemJo.optJSONArray("weekCourse")
                            weekCourseJA?.let { weekCourse ->

                                val itemCourseList = mutableListOf<MyCourseEntity>()
                                for (j in 0 until weekCourse.length()) {
                                    val courseEntity = JsonUtil.toBean(
                                            weekCourse.optJSONObject(j), MyCourseEntity::class.java
                                    )
                                    if (j == 0) {
                                        courseEntity.week = week
                                        courseEntity.weekDay = weekDay
                                    }
                                    itemCourseList.add(courseEntity)
                                }
                                allCourseList.addAll(itemCourseList)
                            }

                        }
                    }
                }
                dataSource = allCourseList
                rvView.onLoadFinish(dataSource, refresh)
            }


            override fun onErrorCode(dataJO: JSONObject?, code: String?, msg: String?) {
                super.onErrorCode(dataJO, code, msg)
                rvView.onRefreshFailed(refresh)
            }

            override fun onHttpError(code: String?, msg: String?) {
                super.onHttpError(code, msg)
                rvView.onRefreshFailed(refresh)
            }
        }
    }

    override fun getRvAdapter(): BaseRecyclerAdapter<MyCourseEntity> {
        return MyCourseAdapter(this, FRAGMENT_COURSE_WEEK_VIP)
    }

    override fun getUrlPath(): String {
        return API_WEEK_COURSE_VIP_LIST
    }

    override fun onItemClick(adapterView: AdapterView<*>?, view: View?, i: Int, l: Long) {
        super.onItemClick(adapterView, view, i, l)
        val entity = adapter[i]

        jumpMyCourseItem(this, null, entity.attend, entity.id, entity.preId)
    }

}
